import { getDialogs,setDialogStatus,deleteDialog,getDialogSlots,updateSlotByAddSlot,querySlotIsUsedByDialog,saveDialogTemplate,getDialogTemplate,startTraining,publish,getTrainResult,getStatusById,getProgress,updateDialogName} from '@/services/dialog';
import { fetchApiListById,slotList} from '@/services/basiccapacity';
export default {
  namespace: 'dialogAdd',
  state: {
    total: null,
    data: [],
    loading: false
  },

  effects: {
    *fetchList({payload,callback}, { call, put }) {
      const response = yield call(getDialogs,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchSlot({payload,callback}, { call, put }) {
      const response = yield call(slotList,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getDialogSlotsBydialogId({payload,callback}, { call, put }) {
      const response = yield call(getDialogSlots,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *addSlotToDialog({payload,callback}, { call, put }) {
      const response = yield call(updateSlotByAddSlot,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *querySlotIsUsedByDialogAndSlotId({payload,callback}, { call, put }) {
      const response = yield call(querySlotIsUsedByDialog,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *saveDialog({payload,callback}, { call, put }) {
      const response = yield call(saveDialogTemplate,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *getDialog({payload,callback}, { call, put }) {
      const response = yield call(getDialogTemplate,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *trainDialog({payload,callback}, { call, put }) {
      const response = yield call(startTraining,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *publishDialog({payload,callback}, { call, put }) {
      const response = yield call(publish,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchTrainResult({payload,callback}, { call, put }) {
      const response = yield call(getTrainResult,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchStatusById({payload,callback}, { call, put }) {
      const response = yield call(getStatusById,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *fetchProgress({payload,callback}, { call, put }) {
      const response = yield call(getProgress,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *updateDialogNameById({payload,callback}, { call, put }) {
      const response = yield call(updateDialogName,payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
      yield put({
        type: 'save',
        payload: response,
      });
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    }
  }
};
